angular.module('imsApp').controller('advManagementController', ['$scope','AdvertisersManager', '$state','$filter','$uibModal', 'AdvManagementManager','ImsDialog', function ($scope,AdvertisersManager, $state,$filter,$uibModal,AdvManagementManager,ImsDialog) {
    $scope.search = {
        pageNo:$state.params.pageNo || 1,
        pageSize:$state.params.pageSize||10,
        campaignName:$state.params.campaignName,
        advertiserId: $state.params.advertiserId||0,
        startDay: $state.params.startDay || null,
        endDay: $state.params.endDay || null
    };
    $scope.rangeDate = {
        startDate:$scope.search.startDay ? moment($scope.search.startDay).format('X'):null,
        endDate:$scope.search.endDay? moment($scope.search.endDay).format('X'):null,
        onSelected: function (data, scope) {  //时间选择确认事件
            $scope.search.startDay = data.startDate?moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD"):null;
            $scope.search.endDay =data.endDate? moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD"):null;
            $state.go($state.current.name, $scope.search)

        }
    };

    $scope.searchClick=function(keyword){
        $scope.search.pageNo=1;
        $scope.search.campaignName=keyword;
        $state.go($state.current.name,$scope.search);
    }

    AdvertisersManager.advertiserSimpleList([1,1000]).then(function(result){
        $scope.advList=result.data.pageList;

   });

    $scope.advChange = function () {
        $state.go('advManagement', $scope.search);
    }
    AdvManagementManager.campaignsList([1,1000]).then(function (result) {
        $scope.campaignList = result.data;
        $scope.campaignObj = $scope.search.campaignId ? $filter("filter")( $scope.campaignList,{campaignId:$scope.search.campaignId})[0] : null;
    })
    $scope.campaignChange = function (campaignObj) {
        if (campaignObj) {
            $scope.search.campaignId = campaignObj.campaignId;
        } else {
            $scope.search.campaignId = null;
        }
        $state.go('advManagement', $scope.search)
    }
    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go('advManagement', $scope.search)
        }
    };
    AdvManagementManager.campaignsList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
        $scope.campaignsList = result.data;
        $scope.paginationConf.totalItems = result.data.total;

    })

    $scope.add=function () {
        var modalInstance = $uibModal.open({
            animation: true,
            size: 'normal',
            templateUrl: 'advManagementAdd.html',
            controller: 'advManagementAddModalController',
            resolve: {
                campaigns:function () {
                    return {};
                },
                types:function () {
                    return $scope.types
                },
                mode:function () {
                    return $scope.search.mode;
                }
            }
        })
        modalInstance.result.then(function (result) {
            if(result=="ok"){
                $state.reload();
            }
        })
    },
    $scope.detail=function (campaigns) {
        var modalInstance = $uibModal.open({
            animation: true,
            size: 'normal',
            templateUrl: 'advManagementDetail.html',
            controller: 'advManagementDetailModalController',
            resolve: {
                campaigns:function () {
                    return campaigns;
                }
            }
        })
        modalInstance.result.then(function (result) {
            if(result=="ok"){
                $state.reload();
            }
        })
    }
    $scope.deletes = function (key) {
        $scope.search.pageNo=1;
        $scope.search[key]=null;
        $state.go($state.current.name, $scope.search)

    }

    $scope.deleteCampaigns=function () {
        var ids=[];
        $scope.campaignsList.pageList.forEach(function (campaigns) {
            if(campaigns.checked){
                ids.push(campaigns.campaignId)
            }
        })
        if(!ids.length){
            return false;
        }else{
            ImsDialog.open({title: "删除确认", message: "您确定要删除数据吗?"}).then(function (a) {
                if (a == 'ok') {
                    AdvManagementManager.campaignsDelete(ids).then(function (result) {
                        $state.reload();
                    })
                }
            })
        }
    }

}])

